﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace BizCollegeWebApp.Models
{
    public partial class BizCollegeDBDataContext
    {
        public int GetFirstQuestionIDOfSurver(int surveyId)
        {
            List<int> ids = SurveyQuestions.Where(sq => sq.SurveryID == surveyId).Select(sq => sq.ID).ToList();
            if (ids.Any())
            {
                ids.Sort();
                return ids[0];
            }
            return Constants.INVALIDID;
        }

        public int GetNextQuestionIdByResponse(int responseID, int currentQuestionId = -1)
        {
            SurveyReponse sr = SurveyReponses.FirstOrDefault(s => s.ID == responseID);
            if (sr == null)
            {
                return Constants.INVALIDID;
            }
            List<int> ids = SurveyQuestions.Where(sq => sq.SurveryID == sr.SurveyID).Select(sq => sq.ID).ToList();
            if (ids.Any())
            {
                if (currentQuestionId == Constants.INVALIDID)
                {
                    return ids.First();
                }
                else
                {
                    ids.Sort();
                    int idx = ids.IndexOf(currentQuestionId);

                    // if it's last one
                    if (idx == ids.Count - 1)
                    {
                        return Constants.INVALIDID;
                    }
                    else
                    {
                        return ids[idx + 1];
                    }
                }
            }
            return Constants.INVALIDID;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="courseId">currently unused. If different survey per course functinality is added it will be used</param>
        /// <returns></returns>
        public Survey GetCourseSurvey(int courseId)
        {
                return Surveys.FirstOrDefault(s => s.IsActive);
        }

        public void DeactivateActiveSurveys()
        {
            foreach (Survey s in Surveys.Where(ss => ss.IsActive))
            {
                s.IsActive = false;
            }
        }

        public int GetPassiveCourseState()
        {
            return this.GetCourseState(Constants.COURSEPASSIVE);
        }

        public int GetUnderReviewCourseState()
        {
            return this.GetCourseState(Constants.COURSEUNDERREVIEW);
        }

        private int GetCourseState(string state)
        {
            CourseState cs = CourseStates.First(cc => cc.StateName == state);
            if (cs == null)
            {
                return -1;
            }
            return cs.ID;
        }

        public IQueryable<Course> GetCourses(string SearchTerm)
        {
            if (string.IsNullOrEmpty(SearchTerm))
            {
                return Courses.AsQueryable();
            }
            else
            {
                return Courses.Where(c => c.CourseName.Contains(SearchTerm) || c.CourseDesciption.Contains(SearchTerm));
            }
        }

        public IQueryable<Article> GetArticles(string Article)
        {
            if (string.IsNullOrEmpty(Article))
            {
                return Articles.AsQueryable();
            }
            else
            {
                return Articles.Where(c => c.ArticleName.Contains(Article) || c.ArticleName.Contains(Article));
            }
        }
        public IQueryable<Survey> GetSurveys(string Survey)
        {
            if (string.IsNullOrEmpty(Survey))
            {
                return Surveys.AsQueryable();
            }
            else
            {
                return Surveys.Where(c => c.Title.Contains(Survey) || c.Title.Contains(Survey));
            }
        }
        
    }
}